package wb_view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;
import javax.swing.JButton;

import model.ParkingAreaManager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class AvailableArea extends JFrame {

	private JPanel contentPane;
	private JLabel jlabelAvail;
	private JTextArea jfieldArea;
	private JButton jbtnCancel;
	private JButton jbtnOK;
	private Thread searchThread;

	private static final String BLANK = " ";
	
	/**
	 * Create the frame.
	 */
	public AvailableArea() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		jlabelAvail = new JLabel("\uC8FC\uCC28 \uAC00\uB2A5 \uAD6C\uC5ED");
		jlabelAvail.setHorizontalAlignment(SwingConstants.CENTER);
		jlabelAvail.setBounds(154, 25, 114, 35);
		contentPane.add(jlabelAvail);
		
		jfieldArea = new JTextArea();
		jfieldArea.setBounds(49, 88, 334, 109);
		contentPane.add(jfieldArea);
		
		jbtnCancel = new JButton("\uAC80\uC0C9\uCDE8\uC18C");
		jbtnCancel.setBounds(80, 215, 97, 23);
		contentPane.add(jbtnCancel);
		
		jbtnOK = new JButton("\uD655 \uC778");
		jbtnOK.setBounds(243, 215, 97, 23);
		contentPane.add(jbtnOK);
		
		final StringBuilder sb = new StringBuilder();
		searchThread = new Thread() {
			
			public void run() {
		
				try {
					
					ArrayList<String> availList = ParkingAreaManager.getInstance().getAvailList();
					
					for(int i = 0; i < availList.size(); ++i) {
						
						String availArea = availList.get(i);
						
						if(i % 4 == 0)
							sb.append("\n");
						
						sb.append(BLANK + availArea + BLANK);
						
						jfieldArea.setText(sb.toString());
						sleep(2000);
					}
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				} 
			};
		};
		searchThread.start();
		
		
		ActionListener al = new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				
				if(ae.getSource()==jbtnCancel) // �˻���ҹ�ư
				{
					searchThread.interrupt();
				}
				
				if(ae.getSource()==jbtnOK) //Ȯ��(�ݱ�)��ư
				{
					dispose();
				}
				
			}
			
				
		};
		jbtnCancel.addActionListener(al);
		jbtnOK.addActionListener(al);
	}
}
